class Solution
{
public:
    string longestCommonPrefix(vector<string> &strs)
    {
        string result = strs[0];
        int n = strs.size();
        for (int i = 1; i < n; ++i)
        {
            result = commonPrefix(result, strs[i]);
            if (result == "")
            {
                break;
            }
        }
        return result;
    }

    string commonPrefix(string s1, string s2)
    {
        int len = min(s1.size(), s2.size());
        int i = 0;
        while (i < len)
        {
            if (s1[i] == s2[i])
            {
                ++i;
            }
            else
            {
                break;
            }
        }
        return s1.substr(0, i);
    }
};